import { Component, OnInit, ViewChild } from '@angular/core';
import { IonContent } from '@ionic/angular';
import { WebsocketService } from '../../../services/websocket.service';
import { LoginInfoService } from '../../../services/login-info.service';
import { HttpService } from '../../../services/http.service';

@Component({
  selector: 'app-customer',
  templateUrl: './customer.page.html',
  styleUrls: ['./customer.page.scss'],
})
export class CustomerPage implements OnInit {
  @ViewChild(IonContent,{static:false}) content: IonContent;
  public userId:any //当前用户身份证
  public username:any //当前用户名字
  public headimg:any //当前用户的头像
  public questionInfo:any //问题内容
  public notaryId:any //对话的公证员ID
  public wsCoketUrl:any //wsc连接地址
  public zxInfo:any = [] //咨询的内容
  public commonInfo = [  //常见问题
    {
      title:'怎样在线办理公证业务？',
      content:'根据您的需要，点击首页的在线公证、视频公证、合同公证的按钮，进行公证。'
    },
    {
      title:'视频公证怎么收费？',
      content:'根据您的具体公证事项进行不同的收费，若要了解详细，请咨询在线的公证员。'
    },
    {
      title:'公证业务服务时间？',
      content:'公证处服务时间为工作日的9:00-17:00，在线公证只需自行操作，视频和合同公证一般为半个小时。'
    },
    {
      title:'一般公证需要多长时间出证？',
      content:'根据不同情况而定，一般为3-5个工作日。'
    },
  ]
  public _index = 1;
  constructor(
    public wsCoket: WebsocketService,
    public HttpService: HttpService,
    public LoginInfoService: LoginInfoService,
  ) { }

  ngOnInit() {
    this.userId =  this.LoginInfoService.getlocal('user').idCard
    this.username =  this.LoginInfoService.getlocal('user').userName
    this.wsCoketUrl = this.HttpService.websockurl() + this.userId
    this.headimg = this.LoginInfoService.getlocal('user').headIcon
   
  }
  //查看是否有公证员
  looknotaryId(){
  
    if(this._index == 1){
      this.wsCoket.createObservableScoket(this.wsCoketUrl);
    }
    this._index++;
    this.acceptwscoket()
    if(this.notaryId == undefined){
      this.HttpService.getArtificial({idCard:this.userId}).then(res =>{
        if(res['code']==200){
          console.log(res,'+++++++')
          this.notaryId = res['item'].id
          let msginfo = '公证员' + res['item'].name + '为您服务'
          this.systemMsg(2,msginfo)
        }else{
          this.systemMsg(2,'暂时没有公证员空闲！您可以留言')
        }
      }).catch(err =>{
        this.systemMsg(2,'不好了，出现BUG了！')
      })
    }
  }
  //wscoket 来回推送
  acceptwscoket(){
    this.wsCoket.getMessage().subscribe(data => {
      console.log(data,'+++++++++++++++')
      if(data.code == '999'){
        this.systemMsg(2,data.info)
      }
    })
  }

  //发送
  towscoket(){
    let info = {
        code: '999',
        userId:this.notaryId,
        name: this.username,
        info: this.questionInfo,
        formId:this.userId
    }
    console.log(this._index,'12332')
    if(this.questionInfo == undefined){
    }else if (this.questionInfo.length < 4 && this._index == 1){
      this.systemMsg(1,this.questionInfo)
      this.systemMsg(2,'您好，暂时不清楚你的问题，请将问题描述清楚点。')
    }else if(this._index !== 1){
      this.wsCoket.sendMessage(info)
      this.systemMsg(1,this.questionInfo)
    }else{
      this.systemMsg(1,this.questionInfo)
      for(let i = 0;i < this.commonInfo.length;i++){
        //如果字符串中不包含目标字符会返回-1
        if(this.commonInfo[i].title.indexOf(this.questionInfo) >= 0){
          this.systemMsg(2,this.commonInfo[i].content)
          break
        }
      }
    }
    this.questionInfo = undefined
  }
  //系统消息
  systemMsg(indexNum:number,info:any){
    let msg = {
      index:indexNum,
      content: info
    }
    this.zxInfo.push(msg)
    this.scrollToBottom(1)
  }
  //点击固定的问题
  toMsg(item?){
    if(item == undefined){
      this.looknotaryId()
    }else{
      this.systemMsg(1,item.title)
      this.systemMsg(2,item.content)
    }
  }
  //滚到底部
  scrollToBottom(int:any) {
    setTimeout(() => {
      this.content.scrollToBottom(1);
    }, int);
  }
  //退出时关闭
  outCus(){
    let info = {
      code: '999',
      userId: this.notaryId,
      name: this.username,
      info: '（此人已经退出在线咨询了！）',
      formId:this.userId
    }
    this.wsCoket.sendMessage(info)
    this.wsCoket.closeWebSocket()
  }
}
